845C - Two TVs - CodeForces Solution


data structures greedy sortings *1500

Please click on ads to support us..

Python Code:

l=[]
for _ in range(int(int(input()))):
    a,b=map(int,input().split())
    l.append([a,1])
    l.append([b+1,-1])
c=0
l.sort()
for x in l:
    c+=x[1]
    if c>2:print('NO');break
else: print('YES')

C++ Code:

// © Urvish Patel

#include<bits/stdc++.h>
using namespace std;

#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define ll long long int

#define endll "\n"

#define f(var,srt,ed) for(int var=srt;var<ed;var++)
#define rf(var,srt,ed) for(int var=srt;var>ed;var--)
#define fe(var,cont) for(auto &var:cont)

#define pb push_back
#define begin begin()
#define end end()

#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/(__gcd(a,b)))

#define YES(x) cout<<"YES"<<x
#define NO(x) cout<<"NO"<<x


void solve()
{
    int n;
    cin>>n;

    vector<pair<int,int>> tv;

    f(i,0,n)
    {
        int l,r;
        cin>>l>>r;

        tv.pb({l,r});
    }

    sort(tv.begin,tv.end);

    int ed1=-1,ed2=-1;

    fe(it,tv)
    {
        int st=it.first,ed=it.second;

        bool check=false;

        if(ed1<st)
        {
            ed1=ed;
            check=true;
        }
        else if(ed2<st)
        {
            ed2=ed;
            check=true;
        }

        if(!check)
        {
            cout<<"NO"<<endl;
            return ;
        }
    }

    cout<<"YES"<<endl;

}


int main()
{
    fast

    int t=1;
    //cin>>t;

    f(_,0,t)
    {
        //cout<<"Case #"<<_<<": ";
        solve();
    }




    return 0;
}


Comments

Submit
0 Comments
More Questions

583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation